package com.example.springboot159.learn.Java8Stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java8Stream {
    public static void main(String[] args) {
        Person p1 = new Person(1, 9000, 21, 1, "河南");
        Person p2 = new Person(2, 2100, 20, 1, "湖北");
        Person p3 = new Person(3, 2050, 26, 2, "陕西");
        List<Person> list = new ArrayList<>(8);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        //过滤工资大于8000
        System.out.println(list.stream().filter(p -> p.getSalary() > 8000).collect(Collectors.toList()));
        //求最大值对应的对象
        System.out.println(list.stream().max(Comparator.comparingInt(Person::getSalary)).get());
        //求最大值对应的对象
        System.out.println(list.stream().max(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getSalary().compareTo(o2.getSalary());
            }
        }));
        //所有员工，工资加1000
        System.out.println(list.stream().map(e -> e.getSalary() + 1000).collect(Collectors.toList()));
        //求和
        System.out.println(list.stream().map(Person::getSalary)
                .reduce(Integer::sum).get());

        System.out.println(list.stream().reduce(0, (sum, p) -> sum += p.getSalary(), (sum1, sum2) -> sum1 + sum2));

        //求最大值
        System.out.println(list.stream().max(Comparator.comparingInt(Person::getSalary)).get().getSalary());

        //求最大值
        System.out.println(list.stream().max(Comparator.comparingInt(Person::getSalary)).get().getSalary());
        //求平均值
        System.out.println(list.stream().collect(Collectors.averagingInt(Person::getSalary)));
        //按照性别分组
        System.out.println(list.stream().collect(Collectors.groupingBy(Person::getSex)));
        //拼接字符串
        System.out.println(list.stream().map(Person::getLocation).collect(Collectors.joining(",")));
        //排序
        System.out.println(list.stream().sorted(Comparator.comparingInt(Person::getSalary)).collect(Collectors.toList()));
        //倒叙
        System.out.println(list.stream().sorted(Comparator.comparingInt(Person::getSalary).reversed()).collect(Collectors.toList()));

        //多次排序
        System.out.println(list.stream().sorted(Comparator.comparingInt(Person::getSalary).reversed().thenComparing(Person::getSalary)).collect(Collectors.toList()));

        //自定义排序
        System.out.println(list.stream().sorted((a,b)-> {
            if (a.getSalary() == b.getSalary()) {
                return a.getAge() - b.getAge();
            } else {
                return a.getSalary() - b.getSalary();
            }
        }).collect(Collectors.toList()));

        //合并与去重
        String[] str1 = {"a","b","c","d"};
        String[] str2 = {"a","b","f","d"};
        System.out.println(Stream.concat(Stream.of(str1),Stream.of(str2)).distinct().collect(Collectors.toList()));

        System.out.println(Stream.concat(Stream.of(str1),Stream.of(str2)).limit(10).collect(Collectors.toList()));

        System.out.println(Stream.concat(Stream.of(str1),Stream.of(str2)).limit(10).skip(3).collect(Collectors.toList()));
    }


}
